﻿using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using Microsoft.Practices.EnterpriseLibrary.Validation.Configuration;
using System;

namespace AsNum.Common.EntLib.Validation.Adapters {
    public class DomainClientAdapter : BaseClientAdapter<DomainValidatorData> {

        public override RuleTypes RuleType {
            get {
                //return RuleTypes.Domain;
                return RuleTypes.Regex;
            }
        }

        public DomainClientAdapter(DomainValidatorData data , Dictionary<string , Tuple<string , string>> map , bool isSub = false)
            : base ( data , map , isSub ) {
            //var a = this.ValidatorData.Domain.Select(d => string.Format("'{0}':1", d.Name.JsSafeString()));
            //this.AddRule(RuleKeys.Domain, string.Format("{{ {0} }}", string.Join(",", a.ToArray())));

            var reg = new Regex ( @"[\^\$\.\*\+\?\=\!\:\|\\\/\(\)\[\]\{\}]" );

            var b = this.ValidatorData.Domain.Select ( d => reg.Replace ( d.Name , (ma) => {
                return @"\" + ma.Groups[0];
            } ) );

            this.AddRule ( RuleKeys.Regex , string.Format ( "/^({0})$/" , string.Join ( "|" , b.ToArray ( ) ) ) );
        }
    }
}
